(function(app){
	app
		.controller("content_06_inputCtrl",["$scope","myFactory",'$http','$stateParams','$state',"$q",'$timeout','$location','$ionicHistory',function($scope,myFactory,$http,$stateParams,$state,$q,$timeout,$location,$ionicHistory){
			//参数定义、接收
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item06
			var searchParam = $stateParams.searchParam;
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			var token=sessionStorage.getItem("token");
			//绑定参数到页面
			$scope.chdrnum01 = chdrnum01;
			$scope.readFlag = false;//阅读协议标志，默认为false
			
			//页面同意阅读协议复选框部分
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
			}
			
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}	
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}
			
/**********************************************初始化函数begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化查询变更项内容
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						itemData = response.data.data.modelMap;
						$scope.item06 = itemData.item06;
						console.log($scope.item06);

						//初始化当前保费逾期未付方式
						$scope.oldAutoPayFlag = itemData.oldAutoPayFlag;
						if($scope.oldAutoPayFlag==null || $scope.oldAutoPayFlag==""){
							$scope.oldPayWay = "无";
						}else{
							if($scope.oldAutoPayFlag=='LAP'){
								$scope.oldPayWay = "效力中止";
							}else if($scope.oldAutoPayFlag=='APL'){
								$scope.oldPayWay = "垫缴保费";
							}
						}
						
						//初始化新的保费逾期未付方式
						if($scope.item06!=null && $scope.item06!=undefined && $scope.item06!=""){ //如果存在记录，则按记录
							var type = "";
							if($scope.item06.autoPayFlag=='LAP' ){
								type = "LAP";
							}else if($scope.item06.autoPayFlag=='APL'){
								type = "APL";
							}
							$("#payWaySelect").val(type);
							$scope.payWaySelect = type;
							$("#payWayInput").val($("#payWaySelect option:selected").text());
						}else{ //否则默认效力中止
							$("#payWaySelect").val("LAP");
							$scope.payWaySelect = "LAP";
							$("#payWayInput").val($("#payWaySelect option:selected").text());
						}
						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
/**********************************************初始化函数end****************************************************/				
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn06Input','overwindowOn06Input','policyCloseXOn06Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-chdrnum-token-timestamp";
				var signStr="itemId="+itemId+"&chdrnum="+chdrnum01+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn06Input = function(){
				modal('close','policyModalOn06Input','overwindowOn06Input','policyCloseXOn06Input');
			}
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			//客户须知
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindowOn06Input');
			}
			$scope.closeTip = function(){
				modal('close','tip_model','overwindowOn06Input');
			}
			
			//选择逾期未付方式下拉框
			$scope.changeWay = function(){
				var options = "";
				options =$("#payWaySelect option:selected");
				$("#payWayInput").val(options.text());
			}
			
			//下一步
			$scope.nextStep = function(){
				myFactory.loadingShow();//显示加载圈
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					myFactory.loadingHide();
					return;
				}
				if($scope.readFlag == false){
					myFactory.alert("","请先同意《申请须知》!","info");
					myFactory.loadingHide();
					return;
				}
				if($("#oldPayWayCode").val()!==""){
		        	var oldPayWayCode = $("#oldPayWayCode").val();
		        	var newPayWayCode = $("#payWaySelect option:selected").val();
		        	if(oldPayWayCode==newPayWayCode){
		        		myFactory.alert("","保费逾期未付选择方式没有改变不需变更","info");
		        		myFactory.loadingHide();
		        	    return false;
		        	}    			
		        }
				
				//封装数据
				var v = $("#payWaySelect option:selected");
				var autoPayFlag = v.val();
				var autoPayFlagNm = v.text();
				var item={
					"itemId":itemId,
			        "autoPayFlag":autoPayFlag,
			        "autoPayFlagNm":autoPayFlagNm
			    }
				
		        myFactory.loadingHide();
		        $ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy
					});
				})
			}
					
		}]);
		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});       
})(app)